package com.caac.core.util;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright caac 2006</p>
 * <p>Company: caac</p>
 * @author <a href="mailto:project@caac.com.cn">Project Group</a>
 * @version 1.0
 */

public final class HttpUtils {
    public static final int COOKIE_VALUE_UNSAVE = -1;
    public static final int COOKIE_VALUE_REMOVE = 0;
    public static final int COOKIE_VALUE_MINUTE = 60;
    public static final int COOKIE_VALUE_HOUR = 3600;
    public static final int COOKIE_VALUE_DAY = 86400;
    public static final int COOKIE_VALUE_MAX = Integer.MAX_VALUE;
    public static final String COOKIE_DEFAULT_PATH = "/";

    public static String getParameter(HttpServletRequest request, String name) {
        return getParameter(request, name, false);
    }

    public static String getParameter(HttpServletRequest request, String name,
                                      boolean canEmpty) {
        return handleString(request.getParameter(name), canEmpty);
    }

    public static boolean getBooleanParameter(HttpServletRequest request,
                                              String name, boolean defaultValue) {
        return handleBoolean(request.getParameter(name), defaultValue);
    }

    public static int getIntParameter(HttpServletRequest request, String name,
                                      int defaultValue) {
        return handleInt(request.getParameter(name), defaultValue);
    }

    public static long getLongParameter(HttpServletRequest request, String name,
                                        long defaultValue) {
        return handleLong(request.getParameter(name), defaultValue);
    }

    public static float getFloatParameter(HttpServletRequest request,
                                          String name, float defaultValue) {
        return handleFloat(request.getParameter(name), defaultValue);
    }

    public static double getDoubleParameter(HttpServletRequest request,
                                            String name, double defaultValue) {
        return handleDouble(request.getParameter(name), defaultValue);
    }

    public static String[] getParameters(HttpServletRequest request,
                                         String name) {
        return getParameters(request, name, false);
    }

    public static String[] getParameters(HttpServletRequest request,
                                         String name, boolean canEmpty) {
        return handleStrings(request.getParameterValues(name), canEmpty);
    }

    public static boolean[] getBooleanParameters(HttpServletRequest request,
                                                 String name,
                                                 boolean defaultValue) {
        return handleBooleans(request.getParameterValues(name), defaultValue);
    }

    public static int[] getIntParameters(HttpServletRequest request,
                                         String name, int defaultValue) {
        return handleInts(request.getParameterValues(name), defaultValue);
    }

    public static long[] getLongParameters(HttpServletRequest request,
                                           String name, long defaultValue) {
        return handleLongs(request.getParameterValues(name), defaultValue);
    }

    public static float[] getFloatParameters(HttpServletRequest request,
                                             String name, float defaultValue) {
        return handleFloats(request.getParameterValues(name), defaultValue);
    }

    public static double[] getDoubleParameters(HttpServletRequest request,
                                               String name, double defaultValue) {
        return handleDoubles(request.getParameterValues(name), defaultValue);
    }

    public static Object getAttribute(HttpServletRequest request, String name) {
        return request.getAttribute(name);
    }

    public static Object getSessionAttribute(HttpServletRequest request,
                                             String name) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return null;
        }
        return session.getAttribute(name);
    }

    public static Object getContextAttribute(HttpServletRequest request,
                                             String name) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return null;
        }
        ServletContext context = session.getServletContext();
        if (context == null) {
            return null;
        }
        return context.getAttribute(name);
    }

    public static void setAttribute(HttpServletRequest request, String name,
                                    Object value) {
        request.setAttribute(name, value);
    }

    public static void setAttribute(HttpServletRequest request, String name,
                                    boolean value) {
        setAttribute(request, name, Boolean.valueOf(value));
    }

    public static void setAttribute(HttpServletRequest request, String name,
                                    int value) {
        setAttribute(request, name, new Integer(value));
    }

    public static void setAttribute(HttpServletRequest request, String name,
                                    long value) {
        setAttribute(request, name, new Long(value));
    }

    public static void setAttribute(HttpServletRequest request, String name,
                                    float value) {
        setAttribute(request, name, new Float(value));
    }

    public static void setAttribute(HttpServletRequest request, String name,
                                    double value) {
        setAttribute(request, name, new Double(value));
    }

    public static void setSessionAttribute(HttpServletRequest request,
                                           String name, Object value) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return;
        }
        if (value == null) {
            session.removeAttribute(name);
        } else {
            session.setAttribute(name, value);
        }
    }

    public static void setSessionAttribute(HttpServletRequest request,
                                           String name, boolean value) {
        setSessionAttribute(request, name, Boolean.valueOf(value));
    }

    public static void setSessionAttribute(HttpServletRequest request,
                                           String name, int value) {
        setSessionAttribute(request, name, new Integer(value));
    }

    public static void setSessionAttribute(HttpServletRequest request,
                                           String name, long value) {
        setSessionAttribute(request, name, new Long(value));
    }

    public static void setSessionAttribute(HttpServletRequest request,
                                           String name, float value) {
        setSessionAttribute(request, name, new Float(value));
    }

    public static void setSessionAttribute(HttpServletRequest request,
                                           String name, double value) {
        setSessionAttribute(request, name, new Double(value));
    }

    public static void setContextAttribute(HttpServletRequest request,
                                           String name, Object value) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return;
        }
        ServletContext context = session.getServletContext();
        if (context == null) {
            return;
        }
        if (value == null) {
            context.removeAttribute(name);
        } else {
            context.setAttribute(name, value);
        }
    }

    public static void setContextAttribute(HttpServletRequest request,
                                           String name, boolean value) {
        setContextAttribute(request, name, Boolean.valueOf(value));
    }

    public static void setContextAttribute(HttpServletRequest request,
                                           String name, int value) {
        setContextAttribute(request, name, new Integer(value));
    }

    public static void setContextAttribute(HttpServletRequest request,
                                           String name, long value) {
        setContextAttribute(request, name, new Long(value));
    }

    public static void setContextAttribute(HttpServletRequest request,
                                           String name, float value) {
        setContextAttribute(request, name, new Float(value));
    }

    public static void setContextAttribute(HttpServletRequest request,
                                           String name, double value) {
        setContextAttribute(request, name, new Double(value));
    }

    public static void removeAttribute(HttpServletRequest request, String name) {
        request.removeAttribute(name);
    }

    public static void removeSessionAttribute(HttpServletRequest request,
                                              String name) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return;
        }
        session.removeAttribute(name);
    }

    public static void removeContextAttribute(HttpServletRequest request,
                                              String name) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return;
        }
        ServletContext context = session.getServletContext();
        if (context == null) {
            return;
        }
        context.removeAttribute(name);
    }

    public static String getSessionId(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        return (session == null) ? null : session.getId();
    }

    public static Cookie[] getCookies(HttpServletRequest request) {
        return request.getCookies();
    }

    public static Cookie getCookie(HttpServletRequest request, String name) {
        if (name == null) {
            return null;
        }
        Cookie cookies[] = request.getCookies();
        if (cookies == null) {
            return null;
        }
        int length = cookies.length;
        for (int i = 0; i < length; i++) {
            if (name.equals(cookies[i].getName())) {
                return cookies[i];
            }
        }
        return null;
    }

    public static void setCookie(HttpServletResponse response, Cookie cookie) {
        if (cookie == null) {
            return;
        }
        response.addCookie(cookie);
    }

    public static void setCookie(HttpServletResponse response, String name,
                                 String value, String path, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath(path);
        cookie.setMaxAge(maxAge);
        setCookie(response, cookie);
    }

    public static void setCookie(HttpServletResponse response, String name,
                                 String value, int maxAge) {
        setCookie(response, name, value, COOKIE_DEFAULT_PATH, maxAge);
    }

    public static void removeCookie(HttpServletResponse response, Cookie cookie) {
        if (cookie == null) {
            return;
        }
        cookie.setValue("");
        cookie.setPath(COOKIE_DEFAULT_PATH);
        cookie.setMaxAge(COOKIE_VALUE_REMOVE);
        setCookie(response, cookie);
    }

    public static void removeCookie(HttpServletResponse response, String name) {
        setCookie(response, name, "", COOKIE_VALUE_REMOVE);
    }

    public static void invalidateSession(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return;
        }
        session.invalidate();
    }

    private static String handleString(String value, boolean canEmpty) {
        if (value == null) {
            return null;
        }
        value = value.trim();
        if (value.length() == 0 && !canEmpty) {
            return null;
        }
        return value;
    }

    private static boolean handleBoolean(String value, boolean defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        value = value.trim();
        if ("true".equalsIgnoreCase(value)) {
            return true;
        } else if ("false".equalsIgnoreCase(value)) {
            return false;
        } else {
            return defaultValue;
        }
    }

    private static int handleInt(String value, int defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        value = value.trim();
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    private static long handleLong(String value, long defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        value = value.trim();
        try {
            return Long.parseLong(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    private static float handleFloat(String value, float defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        value = value.trim();
        try {
            return Float.parseFloat(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    private static double handleDouble(String value, double defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        value = value.trim();
        try {
            return Double.parseDouble(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    private static String[] handleStrings(String[] values, boolean canEmpty) {
        if (values == null) {
            return null;
        }
        int length = values.length;
        for (int i = 0; i < length; i++) {
            values[i] = values[i].trim();
            if (values[i].length() == 0 && !canEmpty) {
                values[i] = null;
            }
        }
        return values;
    }

    private static boolean[] handleBooleans(String[] values,
                                            boolean defaultValue) {
        if (values == null) {
            return null;
        }
        int length = values.length;
        boolean[] returnValues = new boolean[length];
        for (int i = 0; i < length; i++) {
            values[i] = values[i].trim();
            if ("true".equalsIgnoreCase(values[i])) {
                returnValues[i] = true;
            } else if ("false".equalsIgnoreCase(values[i])) {
                returnValues[i] = false;
            } else {
                returnValues[i] = defaultValue;
            }
        }
        return returnValues;
    }

    private static int[] handleInts(String[] values, int defaultValue) {
        if (values == null) {
            return null;
        }
        int length = values.length;
        int[] returnValues = new int[length];
        for (int i = 0; i < length; i++) {
            values[i] = values[i].trim();
            try {
                returnValues[i] = Integer.parseInt(values[i]);
            } catch (NumberFormatException e) {
                returnValues[i] = defaultValue;
            }
        }
        return returnValues;
    }

    private static long[] handleLongs(String[] values, long defaultValue) {
        if (values == null) {
            return null;
        }
        int length = values.length;
        long[] returnValues = new long[length];
        for (int i = 0; i < length; i++) {
            values[i] = values[i].trim();
            try {
                returnValues[i] = Long.parseLong(values[i]);
            } catch (NumberFormatException e) {
                returnValues[i] = defaultValue;
            }
        }
        return returnValues;
    }

    private static float[] handleFloats(String[] values, float defaultValue) {
        if (values == null) {
            return null;
        }
        int length = values.length;
        float[] returnValues = new float[length];
        for (int i = 0; i < length; i++) {
            values[i] = values[i].trim();
            try {
                returnValues[i] = Float.parseFloat(values[i]);
            } catch (NumberFormatException e) {
                returnValues[i] = defaultValue;
            }
        }
        return returnValues;
    }

    private static double[] handleDoubles(String[] values, double defaultValue) {
        if (values == null) {
            return null;
        }
        int length = values.length;
        double[] returnValues = new double[length];
        for (int i = 0; i < length; i++) {
            values[i] = values[i].trim();
            try {
                returnValues[i] = Double.parseDouble(values[i]);
            } catch (NumberFormatException e) {
                returnValues[i] = defaultValue;
            }
        }
        return returnValues;
    }
}
